package com.sservice.service.service.impl;

import com.sservice.banfu.e.AuctionRegister;
import com.sservice.banfu.e.SaleInfo;
import com.sservice.banfu.service.AuctionRegisterService;
import com.sservice.banfu.service.SalePersonService1;
import com.sservice.banfu.vo.SalePersonVo;
import com.sservice.banfu.mapper.SalePersonMapper;
import com.sservice.base.entity.dto.BaseResult;
import com.sservice.common.Constants;
import com.sservice.banfu.e.SalePerson;
import com.sservice.model.entity.Info;
import com.sservice.model.entity.UserWx;
import com.sservice.model.vo.Auction;
import com.sservice.model.vo.InfoVo;
import com.sservice.model.wx.Template;
import com.sservice.model.wx.TemplateParam;
import com.sservice.service.common.AdvancedUtil;
import com.sservice.service.service.IInfoService;
import com.sservice.service.service.ISalePersonService;
import com.sservice.service.service.IUserWxService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class SalePersonServiceImpl implements ISalePersonService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SalePersonService1 salePersonService1;
    @Autowired
    private IUserWxService userWxDao;
    @Autowired
    private AuctionRegisterService auctionRegisterService;
    @Autowired
    private IInfoService iInfoService;
    @Autowired
    private AdvancedUtil advancedUtil;

    @Override
    public List<SalePerson> queryByPage(SalePerson user) {
        return salePersonService1.queryByPage(user);
    }

    @Override
    public List<SalePerson> queryByUserIdAndPage(SalePerson user) {
        return salePersonService1.queryByUserIdAndPage(user);
    }

    @Override
    public SalePerson queryUserByPhone(String phone) {
        logger.info("service方法进入----");
        return null;
    }

    @Override
    public BaseResult auctionRegisterDetail(AuctionRegister auctionRegister) {
        //竞拍团队
        Auction auction = new Auction();
        String authorId = auctionRegister.getCom();
        auctionRegister.setCom(null);
        if ("team".equals(auctionRegister.getType1())) {
            List<AuctionRegister> list = auctionRegisterService.query(auctionRegister);
            if (list.size() > 0) {
                auction.setAuctionRegister(list.get(0));
                if (authorId == null) {
                    authorId = list.get(0).getOwnerId();
                }
            }else {
                return new BaseResult(false, auction);
            }
            //作品
            InfoVo iv = new InfoVo();
            iv.setType1(5);
            iv.setAuthorId(authorId);
            List<Info> list1 = iInfoService.queryByPage(iv);
            auction.setWorks(list1);
            //成员
            SalePersonVo salePersonVo = new SalePersonVo();
            salePersonVo.setType1(4);
            salePersonVo.setStatus("" + list.get(0).getId());
            auction.setMembers(salePersonService1.page(salePersonVo));
        } else if ("author".equals(auctionRegister.getType1())) {
            List<AuctionRegister> list = auctionRegisterService.query(auctionRegister);
            if (list.size() > 0) {
                auction.setAuctionRegister(list.get(0));
            }
        }
        BaseResult lr = new BaseResult(true, auction);
        return lr;
    }


    @Override
    public int deleteByPrimaryKey(Integer id) {
        return salePersonService1.deleteById(id);
    }

    @Override
    public int insert(SalePerson record) {
        return salePersonService1.save(record);
    }

    @Override
    public int insertSelective(SalePerson record) {
        return salePersonService1.save(record);
    }

    @Override
    public Object registerByShare(SalePersonVo record) {

        SaleInfo res = salePersonService1.selectByOpenId(record.getOpenid());
        if (res != null) {
            return "用户已经注册!";
        }
        //int newJoin = salePersonMapper.insertSelective(record);
        if (record.getUserId() != null) {
            SalePersonVo spShare = salePersonService1.selectByPrimaryKey(Integer.valueOf(record.getUserId()));
            //spShare.setLevel("1");
            spShare.setSalesCount(spShare.getSalesCount() + 1); //累加销售员数量
            if (true) {
                SaleInfo sp = selectByOpenid(record.getOpenid());
                SalePersonVo spParent = salePersonService1.selectByPrimaryKey(Integer.valueOf(record.getUserId())); // 通过unionid登录
                UserWx uwManager = new UserWx();
//                uwManager.setUnionid(spParent.getOpenid());
                uwManager.setUnitid(Constants.WXUserTypeWX);
                UserWx ux1Manager = userWxDao.selectByUnionid(uwManager);
                if (ux1Manager != null && ux1Manager.getOpenid() != null) { //系统包含微信账户
                    logger.debug("系统包含微信账户->" + ux1Manager.getOpenid());
                    Template tem = new Template();
                    tem.setTemplateId(Constants.tmplNewVipJoin);
                    tem.setToUser(ux1Manager.getOpenid());
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                    tem.setUrl(Constants.domain + "/wx/user/index.html");
                    List<TemplateParam> paras = new ArrayList<TemplateParam>();
                    paras.add(new TemplateParam("first", "您名下新增加销售员" + record.getUserName() + "!", "#333333"));
                    paras.add(new TemplateParam("keyword1", "HP-VIP-" + sp.getId(), "#333333"));
                    paras.add(new TemplateParam("keyword2", sdf.format(new Date()), "#333333"));
                    paras.add(new TemplateParam("remark", "点击查看!", "#333333"));
                    tem.setTemplateParamList(paras);
                    advancedUtil.senderTmplSms(tem);
                }
                return 0;
            } else {
                return "更新销售员数量失败!";
            }
        }
        return 0;
    }

    @Override
    public SalePerson selectByPhone(SalePerson user) {
        return salePersonService1.selectByPhone(user);
    }

    @Override
    public SaleInfo selectByOpenid(String openid) {
        return salePersonService1.selectByOpenId(openid);
    }


    @Override
    public int updateByPrimaryKeySelective(SalePerson record) {
        return salePersonService1.update(record);
    }

    @Override
    public List<SalePerson> querySaleManager(SalePersonVo sp) {
        return salePersonService1.querySaleManager(sp);
    }

    @Override
    public int querySaleManagerCount(SalePerson sp) {
        return salePersonService1.querySaleManagerCount(sp);
    }

    @Override
    public int querySaleCashCount(SalePerson user) {
        return salePersonService1.querySaleCashCount(user);
    }

    @Override
    public List<SalePerson> querySaleCashByPage(SalePerson user) {
        return salePersonService1.querySaleCashByPage(user);
    }


    @Override
    public int queryCount(SalePerson user) {
        return salePersonService1.queryCount(user);
    }
}
